package com.whu.helpluojia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whu.helpluojia.common.BaseContext;
import com.whu.helpluojia.dao.ConsultDao;
import com.whu.helpluojia.dao.RecordDao;
import com.whu.helpluojia.domain.Consult;
import com.whu.helpluojia.domain.MyPublish;
import com.whu.helpluojia.domain.Record;
import com.whu.helpluojia.domain.User;
import com.whu.helpluojia.service.ConsultService;
import com.whu.helpluojia.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 92948
 * @description 针对表【consult】的数据库操作Service实现
 */
@Service
public class ConsultServiceImpl extends ServiceImpl<ConsultDao, Consult> implements ConsultService {

    @Autowired
    ConsultDao consultDao;

    @Autowired
    UserService userService;

    @Autowired
    RecordDao recordDao;

    @Override
    public void addConsult(Consult consult) {
        Long userId= BaseContext.getCurrentId();

        consult.setTime(new Date());

        consult.setSenderId(userId);

        System.out.println("consult:"+consult);

        consultDao.insert(consult);
    }

    @Override
    public Consult queryConsultById(Long id){
        return consultDao.selectById(id);
    }

    @Override
    public List<Consult> getByDate(Date dateTime){
        LambdaQueryWrapper<Consult> lqw = new LambdaQueryWrapper<Consult>();
        lqw.eq(Consult::getTime,dateTime);
        return consultDao.selectList(lqw);
    }

    @Override
    public List<Consult> getByUser(Long userId){
        LambdaQueryWrapper<Consult> lqw = new LambdaQueryWrapper<Consult>();
        lqw.eq(Consult::getSenderId,userId);
        return consultDao.selectList(lqw);
    }

    @Override
    public List<Consult> getByType(String type){
        LambdaQueryWrapper<Consult> lqw = new LambdaQueryWrapper<Consult>();
        lqw.eq(Consult::getType,type);
        return consultDao.selectList(lqw);
    }

    @Override
    public List<Consult> getByContent(String content){
        LambdaQueryWrapper<Consult> lqw = new LambdaQueryWrapper<Consult>();
        lqw.like(Consult::getContent,content);
        return consultDao.selectList(lqw);
    }

    @Override
    public List<Consult> getByTitle(String title){
        LambdaQueryWrapper<Consult> lqw = new LambdaQueryWrapper<Consult>();
        lqw.like(Consult::getTitle,title);
        return consultDao.selectList(lqw);
    }

    @Override
    public int deleteConsult(Long id) {
        return consultDao.deleteById(id);
    }



    @Override
    public List<User> getRecommend(String type) {
        LambdaQueryWrapper<Record> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Record::getType,type);
        List<Record> records=recordDao.selectList(queryWrapper);
        Map<Long,Integer> map=new HashMap<>();
        for(Record record:records){
            if(map.containsKey(record.getUserId())){
                map.put(record.getUserId(),map.get(record.getUserId())+1);
            }else{
                map.put(record.getUserId(),1);
            }
        }
        map =  map.entrySet().stream()
                .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
                .collect(
                        Collectors.toMap(x -> x.getKey(), x -> x.getValue(), (x1, x2) -> x2, LinkedHashMap::new));

        List<Long> ids=new ArrayList<>();

        int i=0;
        for (Map.Entry<Long, Integer> entry : map.entrySet()) {
            if(i<5){
                ids.add(entry.getKey());
            }
        }

        List<User>users=userService.listByIds(ids);
        return users;
    }
    public MyPublish copyData(MyPublish myPublish,Consult consult,User user){
        myPublish.setId(consult.getId());
        myPublish.setCategory("校园咨询");
        myPublish.setContent(consult.getContent());
        myPublish.setSex(user.getGender());
        myPublish.setSenderName(user.getName());
        myPublish.setTime(consult.getTime());
        myPublish.setType(consult.getType());
        return myPublish;
    }
    @Override
    public MyPublish getConsultDetail(Long id,Long userId){

        Consult consult=consultDao.selectById(id);

        User user=userService.selectUser(userId);

        MyPublish myPublish=new MyPublish();

        myPublish=copyData(myPublish,consult,user);

        return myPublish;
    }
    @Override
    public List<Consult> selectAll(){
        List<Consult> list = consultDao.selectList(null);
        return list;
    }
    @Override
    public List<MyPublish> getMyDetail(Long userId){
        LambdaQueryWrapper<Consult> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Consult::getSenderId,userId);
        List<Consult>consultList=consultDao.selectList(lambdaQueryWrapper);

        User user=userService.selectUser(userId);

        List<MyPublish> myPublishes=new ArrayList<>();

        for(Consult consult:consultList){
            MyPublish myPublish=new MyPublish();
            myPublish=copyData(myPublish,consult,user);
            myPublishes.add(myPublish);
        }
        return myPublishes;
    }
}
